package com.woniu.mealsService.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.dto.StoreViewDto;
import com.woniu.mealsService.mapper.StoreVIewMapper;
import com.woniu.mealsService.model.OrderProduct;
import com.woniu.mealsService.model.Store;
import com.woniu.mealsService.model.StoreProduct;
import com.woniu.mealsService.model.StoreView;
import com.woniu.mealsService.service.*;
import com.woniu.mealsService.util.Data;
import com.woniu.server.RedisKey;
import com.woniu.server.RedisUtil;

import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jiang
 */
@Service
public class StoreViewServiceImpl implements StoreViewService {
    @Autowired
    private CommentService commentService;
    @Autowired
    private OrderProductService orderProductService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private StoreVIewMapper storeVIewMapper;
    @Autowired
    private StoreService storeService;
    @Autowired
    private StoreProductService storeProductService;
    // 使用cron表达式定义每天凌晨0点执行任务
    @Override
    @Transactional
    @XxlJob("calculateStoreScore")
    public void calculateStoreScore() throws Exception {
        List<Store> list = storeService.list(new LambdaQueryWrapper<Store>().eq(Store::getDeleteFlag, 0));
        List<Long> storeIds = list.stream().map(Store::getStoreId).collect(Collectors.toList());
        for (Long storeId : storeIds) {
            orderProductService.calculateSales(storeId);
        }
    }
    @Override
    public StoreViewDto getCalculateStoreDailySales(Long storeId,String date) throws Exception {
        StoreViewDto storeViewDto = new StoreViewDto();
        String format = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        if(format.equals(date)){
            orderProductService.calculateSales(storeId);
        }
        StoreView storeView = storeVIewMapper.selectOne(storeId, date);
        if (storeView == null) {
            throw new RuntimeException("没有数据");
        }
        BeanUtil.copyProperties(storeView, storeViewDto);
        System.out.println(storeView);
        List<String> list = JSONUtil.toList(storeView.getPopularProduct(), String.class);
        if (list.size() == 0){
            storeViewDto.setProductName("暂无数据");
        }else {
            Long storeProductId = Long.parseLong(list.get(0));
            StoreProduct one = storeProductService.getOne(new LambdaQueryWrapper<StoreProduct>().eq(StoreProduct::getStoreId, storeId).eq(StoreProduct::getStoreProductId,storeProductId));
            System.out.println(one);
            storeViewDto.setProductName(one.getProductName());
        }
        return storeViewDto;
    }

    @Override
    public List<StoreViewDto> getCalculateStoreMonthSales(Long storeId,String date) throws Exception {
        LocalDate parse = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate firstDay = parse.withDayOfMonth(1);
        LocalDate lastDay = parse.withDayOfMonth(parse.lengthOfMonth());
        System.out.println(firstDay+" "+lastDay);
        List<StoreView> storeViews = storeVIewMapper.selectList(storeId,firstDay, lastDay);
        List<StoreViewDto> storeViewDtos = BeanUtil.copyToList(storeViews, StoreViewDto.class);
        return storeViewDtos;
    }

    @Override
    public void calculateStoreDailyBestSales() throws Exception {

    }

    @Override
    public void calculateStoreMonthBestSales() throws Exception {

    }

    @Override
    public void calculateStoreDailySalesDetail() throws Exception {

    }

    @Override
    public com.github.pagehelper.PageInfo<StoreView> searchPageMonthStoreView(Long storeId, String date, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        LocalDate parse = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate firstDay = parse.withDayOfMonth(1);
        LocalDate lastDay = parse.withDayOfMonth(parse.lengthOfMonth());
        List<StoreView> storeViews = storeVIewMapper.selectList(storeId,firstDay, lastDay);
        com.github.pagehelper.PageInfo<StoreView> storeViewPageInfo = new com.github.pagehelper.PageInfo<>(storeViews);
        return storeViewPageInfo;
    }


}
